package com.quanyan.ic.vendingMachine.dao.impl;

import com.quanyan.ic.vendingMachine.dao.VmUpgradeDao;
import com.quanyan.ic.vendingMachine.mapper.VmUpgradeEntityMapper;
import com.quanyan.ic.vendingMachine.mapper.VmUpgradeStatusEntityMapper;
import com.quanyan.ic.vendingMachine.mapper.customSQL.VmUpgradeMapper;
import com.quanyan.ic.vendingMachine.mapper.entity.VmUpgradeEntity;
import com.quanyan.ic.vendingMachine.mapper.entity.VmUpgradeStatusEntity;
import com.quanyan.ic.vendingMachine.mapper.entity.VmUpgradeStatusEntityExample;
import com.quanyan.ic.vendingMachine.model.UpgradeVersionResp;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

/**
 * Created by Administrator on 2017/8/23.
 */
@Repository
public class VmUpgradeDaoImpl implements VmUpgradeDao{

    @Autowired
    private VmUpgradeMapper vmUpgradeMapper;
    @Autowired
    private VmUpgradeEntityMapper vmUpgradeEntityMapper;
    @Autowired
    private VmUpgradeStatusEntityMapper vmUpgradeStatusEntityMapper;

    @Override
    public UpgradeVersionResp findUpgrade(String fileType) {
        if ( StringUtils.isEmpty(fileType) ) {
            return null;
        }
        //根据当前的number查找版本，

        return    vmUpgradeMapper.findFileNameCurrent(fileType);
    }

    @Override
    public void insertFile(UpgradeVersionResp req) {
        VmUpgradeEntity vmUpgradeEntity = new VmUpgradeEntity();
        vmUpgradeEntity.setDownloadUrl(req.getDownloadUrl());
        vmUpgradeEntity.setFileType(req.getFileType());
        vmUpgradeEntity.setIsUpgrade((byte) req.getIsUpgrade());
        vmUpgradeEntity.setVersionCode(req.getVersionCode());
        vmUpgradeEntityMapper.insert(vmUpgradeEntity);
    }

    @Override
    public void insertVmUpgradeStatus(VmUpgradeStatusEntity entity) {
        vmUpgradeStatusEntityMapper.insert(entity);
    }

    @Override
    public void updateUpgradeRecord(VmUpgradeStatusEntity vmUpgradeStatusEntity) {
        VmUpgradeStatusEntityExample example=new VmUpgradeStatusEntityExample();
        example
                .createCriteria()
                .andVmNumberEqualTo(vmUpgradeStatusEntity.getVmNumber());
        vmUpgradeStatusEntityMapper.updateByExampleSelective(vmUpgradeStatusEntity,example);

    }



}
